package treqs.servlet;

import java.io.IOException;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import treqs.model.HibernateUtil;
import treqs.model.Requirement;
import treqs.model.Requirementpriority;
import treqs.model.Requirementstatus;

/**
 * Servlet implementation class RequirementManageServlet
 */
public class RequirementManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequirementManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{			
			response.getWriter().write("Starting...<br>");
	
			EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
			EntityManager em = emf.createEntityManager();
			EntityTransaction tx = em.getTransaction();
			tx.begin();
			
			Requirement requirement = null;
			Boolean createNewRequirement = false;
			if (null != request.getParameter("id"))
			{		
				int requirementID = Integer.parseInt(request.getParameter("id"));
				requirement = em.find(Requirement.class, requirementID);
			}
			if (null == requirement)
			{		
				createNewRequirement = true;
				requirement = new Requirement();
			}

			requirement.setUid(request.getParameter("UID"));
			requirement.setName(request.getParameter("name"));
			requirement.setDescription(request.getParameter("description"));
			requirement.setRationale(request.getParameter("rationale"));
			requirement.setComment(request.getParameter("comment"));
			requirement.setRevision(1);
			
			int priority = Integer.parseInt(request.getParameter("priority"));
			Requirementpriority requirementPriority = em.find(Requirementpriority.class, priority);
			requirement.setRequirementpriority(requirementPriority);

			int status = Integer.parseInt(request.getParameter("status"));
			Requirementstatus requirementStatus = em.find(Requirementstatus.class, status);
			requirement.setRequirementstatus (requirementStatus);

			requirement.setCreationDate(new Date());
			if (createNewRequirement)
			{		
				em.persist(requirement);
			}
			else
			{		
				em.flush();
			}

			tx.commit();
			em.close();
	
			response.getWriter().write("Done!<br>");
			response.sendRedirect("requirement/index.jsp");
		}
		catch (Exception ex)
		{
			response.getWriter().write("No Luck!<br>");
			response.getWriter().write(ex.toString());
			return;
		}

	}

}
